#include "../main.h"

//初始化
void init_work()
{
    InitQueue(&work_msg_Queue);
    pool = threadpool_init(5, 10);
    // msgmap = (Map *)malloc(sizeof(Map));
    // msgmap->head = NULL;
}

//添加原始消息
void append_srcdata(int sockfd, char *msg, int len)
{
    char *data = (char *)malloc(len);
    memset(data, 0, len);
    memcpy(data, msg, len);
    EnQueue(&work_msg_Queue, data, len, sockfd);
    free(data);
    data = NULL;
    threadpool_add_job(pool, process_work_Msg, &work_msg_Queue);
    usleep(10 * 1000);
}

//处理消息
void *process_work_Msg(void *arg)
{
    Queue *c_msgQueue = (Queue *)arg;
    Msgbody *msgbody = DeQueue(c_msgQueue);
    if (msgbody != NULL)
    {
        //printf("msgbody id %d,length:%d\n", msgbody->id, msgbody->length);
        decode(msgbody->id, msgbody->data, msgbody->length);
        if (msgbody->data != NULL)
        {
            free(msgbody->data);
            msgbody->data = NULL;
        }
        free(msgbody);
        msgbody = NULL;
    }
    else
    {
        OUT_DEBUG(DEBUG, "msgbody not exist\n");
    }
    return 0;
}
